package com.hkbigdata.sink;

import com.alibaba.fastjson.JSON;
import com.hkbigdata.entry.WaterSensor;
import org.apache.flink.api.common.serialization.SimpleStringSchema;
import org.apache.flink.streaming.api.datastream.DataStreamSink;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.connectors.kafka.FlinkKafkaProducer;

import java.util.Arrays;
import java.util.List;

/**
 * @author liuanbo
 * @creat 2023-04-19-19:08
 * @see 2194550857@qq.com
 */
public class Flink01_Sink_Kafka {
    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.setParallelism(1);


        List<WaterSensor> waterSensors = Arrays.asList(
                new WaterSensor("ws_001", 1607527992000L, 20),
                new WaterSensor("ws_001", 1607527992001L, 22),
                new WaterSensor("ws_001", 1607527992002L, 33),
                new WaterSensor("ws_002", 1607527992003L, 44),
                new WaterSensor("ws_002", 1607527992004L, 22),
                new WaterSensor("ws_002", 1607527992001L, 50)
        );


        DataStreamSink<String> watersensor = env.fromCollection(waterSensors)
                .map(JSON::toJSONString).
                        addSink(new FlinkKafkaProducer<String>("hadoop102:9092", "watersensor", new SimpleStringSchema()));

        env.execute();


    }
}
